package com.leyou.user.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.user.interceptor.LoginInterceptor;
import com.leyou.user.pojo.Collect;
import com.leyou.user.pojo.User;
import com.leyou.user.pojo.UserAddress;
import com.leyou.user.pojo.UserDetail;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUser(@PathVariable("data") String data, @PathVariable(value = "type") Integer type) {

        Boolean b = this.userService.checkUser(data, type);
        if (b == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(b);
    }

    @PostMapping("code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
        this.userService.sendCode(phone);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("register")
    public ResponseEntity<Void> registerUser(@Valid User user, @RequestParam("code") String code) {
        Boolean b = this.userService.register(user, code);
        if (b == null || !b) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/query")
    public ResponseEntity<User> queryUser(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        User user = this.userService.queryUser(username, password);
        if (user == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(user);
    }

    /**
     * 获取用户收藏列表
     *
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/collection/list")
    public ResponseEntity<PageResult<Collect>> queryCollectionByUserId(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "8") Integer rows) {
        PageResult<Collect> pageResult = this.userService.queryCollectionByUserId(page, rows);
        if (CollectionUtils.isEmpty(pageResult.getItems())) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 添加用户的个人信息，根据ID
     *
     * @param
     * @param userDetail
     * @return
     * @LQB
     */
    @PostMapping("detail")
    public ResponseEntity<Void> saveUserDetail(UserDetail userDetail) {
        Boolean b = this.userService.saveUserDetail(userDetail);
        if (b == null && !b) {
            //参数错误
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 查询userDetail
     * @return
     */
    @GetMapping("detail/userDetail")
    public ResponseEntity<UserDetail> queryUserDetail() {
        Long id = LoginInterceptor.getLoginUserInfo().getId();
        UserDetail userDetail = this.userService.queryUserDetail(id);
        if (userDetail == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(userDetail);

    }

    /***
     * 增加用户收货地址
     * @LQB
     * @param userAddress
     * @return
     */
    @PostMapping("/detail/address")
    public ResponseEntity<Void> addUserAddress(UserAddress userAddress) {
        Boolean b = this.userService.addUserDetail(userAddress);
        if (b == null && !b) {
            //参数错误
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 查詢用戶地址
     * @return
     */
    @GetMapping("detail/userAddress")
    public ResponseEntity< List<UserAddress>> queryUserAddress(){
        Long id = LoginInterceptor.getLoginUserInfo().getId();
        List<UserAddress> userAddresses=this.userService.queryUserAddress(id);
        if(CollectionUtils.isEmpty(userAddresses)){
            return ResponseEntity.noContent().build();
        }
      return ResponseEntity.ok(userAddresses);
    }
}
